package co2.bean;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

import org.hibernate.id.UUIDHexGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.dao.DataIntegrityViolationException;

import co2.engine.DAOInterface;
import co2.engine.SessionValidation;
import co2.pojos.AnggotaKelompok;
import co2.pojos.Kelompok;
import co2.pojos.MstUser;

@ManagedBean
@ViewScoped
public class KelompokBean extends AbstractBean implements Serializable {
	private static final long serialVersionUID = 1L;
	

	@Autowired
	@Qualifier("daoI")
	private DAOInterface dao;
	
	private MstUser user;
	private String userId1;
	private String userId2;
	private String userId3;
	private String userId4;
	
	private Kelompok kelompok;
	

	private List<MstUser> initList;
	private List<MstUser> listUser;
	private List<MstUser> listUser2;
	private List<MstUser> listUser3;
	private List<MstUser> listUser4;
	
	
	@PostConstruct
	public void postCon(){
		kelompok = new Kelompok();
		kelompok.setId(new UUIDHexGenerator().generate(null, null).toString().toUpperCase());
		SignInBean signInBean = SessionValidation.checkSession();
		if(signInBean!=null){
			user = signInBean.getTheUser();
			initList = dao.getSiswaYgBelumPunyaKelompok(user.getUserId());
			listUser = initList;
			listUser2 = initList;
			listUser3 = initList;
			listUser4 = initList;
		}
	}
	public boolean isi(String str){
		if(str!=null && !str.equals("")){
			return true;
		}
		return false;
	}
	public void changeList1(){
		listUser2 = new ArrayList<MstUser>();
		listUser3 = new ArrayList<MstUser>();
		listUser4 = new ArrayList<MstUser>();
		if(isi(userId1)){
			for(MstUser mu:initList){
				if(!mu.getUserId().equals(userId1)){
					listUser2.add(mu);
					listUser3.add(mu);
					listUser4.add(mu);
				}
			}
		}else{
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_FATAL,"Pilih siswa terlebih dahulu",""));
		}
	}
	
	public void changelist2(){
		listUser3 = new ArrayList<MstUser>();
		listUser4 = new ArrayList<MstUser>();
		if(isi(userId1)){
			for(MstUser mu:initList){
				if(!mu.getUserId().equals(userId1) && !mu.getUserId().equals(userId2)){
					listUser3.add(mu);
					listUser4.add(mu);
				}
			}
		}else{
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_FATAL,"Mohon isi Secara Urut, Tentukan siswa no 2 dulu",""));
		}
	}
	
	public void changelist3(){
		listUser4 = new ArrayList<MstUser>();
		if(isi(userId1)&&isi(userId2)){
			for(MstUser mu:initList){
				if(!mu.getUserId().equals(userId1) && !mu.getUserId().equals(userId2) && !mu.getUserId().equals(userId3)){
					listUser4.add(mu);
				}
			}
		}else{
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_FATAL,"Mohon isi Secara Urut, Tentukan siswa no 2 dan no 3 dulu",""));
		}
	}
	public void changelist4(){
		if(!isi(userId1)&&!isi(userId2)&&!isi(userId3)){
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_FATAL,"Mohon isi Secara Urut, Tentukan siswa no 2, no 3, dan no 4 dulu",""));

		}
	}
	public void savek(){
		try {
			dao.save(kelompok);
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,"Saved",""));
		} catch (DataIntegrityViolationException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void save(){
		try{
			dao.save(kelompok);
			
			AnggotaKelompok a1 = new AnggotaKelompok();
			a1.setId(new UUIDHexGenerator().generate(null, null).toString().toUpperCase());
			a1.setKelompok(kelompok);
			a1.setUser(user);
			dao.save(a1);
			
			if(userId1!=null){
				AnggotaKelompok a2 = new AnggotaKelompok();
				a2.setId(new UUIDHexGenerator().generate(null, null).toString().toUpperCase());
				a2.setKelompok(kelompok);
				MstUser mu1 = new MstUser();
				mu1.setUserId(userId1);
				a2.setUser(mu1);
				dao.save(a2);
			}
			if(userId2!=null){
				AnggotaKelompok a3 = new AnggotaKelompok();
				a3.setId(new UUIDHexGenerator().generate(null, null).toString().toUpperCase());
				a3.setKelompok(kelompok);
				MstUser u3 = new MstUser();
				u3.setUserId(userId2);
				a3.setUser(u3);
				dao.save(a3);
			}
			
			if(userId3!=null){
				AnggotaKelompok a4 = new AnggotaKelompok();
				a4.setId(new UUIDHexGenerator().generate(null, null).toString().toUpperCase());
				a4.setKelompok(kelompok);
				MstUser u4 = new MstUser();
				u4.setUserId(userId3);
				a4.setUser(u4);
				dao.save(a4);
			}
			
			if(userId4!=null){
				AnggotaKelompok a5 = new AnggotaKelompok();
				a5.setId(new UUIDHexGenerator().generate(null, null).toString().toUpperCase());
				a5.setKelompok(kelompok);
				MstUser u5 = new MstUser();
				u5.setUserId(userId4);
				a5.setUser(u5);
				dao.save(a5);
			}
			
			String path = FacesContext.getCurrentInstance().getExternalContext().getRequestContextPath();
			FacesContext.getCurrentInstance().getExternalContext().redirect(path+"/jsf/target/lks.jsf");
			
		}catch(Exception e){
			e.printStackTrace();
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"Eror Saving Kelompok: "+e.getMessage(),""));
		}
	}

	public DAOInterface getDao() {
		return dao;
	}


	public void setDao(DAOInterface dao) {
		this.dao = dao;
	}


	public MstUser getUser() {
		return user;
	}


	public void setUser(MstUser user) {
		this.user = user;
	}


	public String getUserId1() {
		return userId1;
	}


	public void setUserId1(String userId1) {
		this.userId1 = userId1;
	}


	public String getUserId2() {
		return userId2;
	}


	public void setUserId2(String userId2) {
		this.userId2 = userId2;
	}


	public String getUserId3() {
		return userId3;
	}


	public void setUserId3(String userId3) {
		this.userId3 = userId3;
	}


	public String getUserId4() {
		return userId4;
	}


	public void setUserId4(String userId4) {
		this.userId4 = userId4;
	}


	public Kelompok getKelompok() {
		return kelompok;
	}


	public void setKelompok(Kelompok kelompok) {
		this.kelompok = kelompok;
	}
	public List<MstUser> getListUser() {
		return listUser;
	}


	public void setListUser(List<MstUser> listUser) {
		this.listUser = listUser;
	}

	public List<MstUser> getListUser2() {
		return listUser2;
	}

	public void setListUser2(List<MstUser> listUser2) {
		this.listUser2 = listUser2;
	}

	public List<MstUser> getListUser3() {
		return listUser3;
	}

	public void setListUser3(List<MstUser> listUser3) {
		this.listUser3 = listUser3;
	}

	public List<MstUser> getListUser4() {
		return listUser4;
	}

	public List<MstUser> getInitList() {
		return initList;
	}

	public void setInitList(List<MstUser> initList) {
		this.initList = initList;
	}

	public void setListUser4(List<MstUser> listUser4) {
		this.listUser4 = listUser4;
	}

	
	
}
